import { defineStore } from 'pinia'
import { changeTime } from "../util/timer"
import { login, register } from "../api/login/index"
import { getEssayList, addEssay } from "../api/essay/index"
import eventBus from "../util/eventBus"

type infoType = {
    id: string,
    msg: string,
    sendTime: string,
    showTime: string
}

type userInfoType = {
    address?: string,
    createTime?: string,
    email?: string,
    nickname?: number,
    password?: string,
    phone?: string,
    token?: string,
    username: string,
}
type resultType = {
    data: any,
    status: any
}
type pageInfo = {
    page: number,
    pagesize: number,
    keyword?: string
}
type essay = {
    id: number,
    title: string,
    user: string,
    createTime: string,
    des: string,
}
// type TaddEssay = {
//     title: string,
//     des: string,
//     user: string
// }

// useStore 可以是 useUser、useCart 之类的任何东西
// 第一个参数是应用程序中 store 的唯一 id
export const userStore: any = defineStore('user', {
    // other options...
    state: () => ({
        userInfo: {} as userInfoType,
        messageList: [] as Array<infoType>,
        socket: {
            close: () => { }
        },
        essayList: [] as Array<essay>
    }),
    actions: {
        async registerUser(data: userInfoType) {
            console.log("注册用户", data);
            const result = <resultType> await register({
                username: data.username,
                password: data.password
            })
            return result
        },
        // 根据用户名密码获取用户信息(传递的用户信息为‘’ 但是存在token则走token免登)
        async saveUserInfo(data: userInfoType) {
            const result = <resultType>await login({
                username: data.username,
                password: data.password
            })
            console.log("valvalvalvalvalvalvalval", result);
            if (result.status.code === 0) {
                sessionStorage.setItem("cxl_Token", result.data.token as string) // 存放token到缓存
                this.userInfo = result.data as userInfoType
                eventBus.emit("connectSocket") // 触发socket连接
            } else {
                sessionStorage.setItem("cxl_Token", '') // 存放token到缓存
            }
        },
        // 保存聊天消息
        saveMessage(val: string) {
            let info: infoType = JSON.parse(val)
            info.showTime = changeTime(info.sendTime)
            console.log("接收到消息", info);
            this.messageList.push(info as infoType)
        },
        // 保存socket对象
        saveSocket(val: any) {
            this.socket.close()
            this.socket = val
        },
        // 查询文章
        async selectEssay(val: pageInfo, type: string) {
            if (type === 'update') {
                console.log("是否更新数据");
                this.essayList = []
            }
            const result: any = await getEssayList(val)
            this.essayList.push(...result.data)
            console.log("查询文章", this.essayList);
            return result.data
        },
        // 新增文章
        async addEssayHandler(val: essay) {
            console.log(val);
            const result: any = await addEssay(val)
            console.log("查询文章", result);
            return result.data
        }
    },
})